library(jpeg)
surface <- readJPEG("resim.jpg")
str(surface)
surface_ras <- as.raster(surface)
par(mfrow=c(1,1))
plot(surface_ras)
par(mfrow=c(3,1))
r<-apply(surface[,,1],2,rev)
g<-apply(surface[,,2],2,rev)
b<-apply(surface[,,3],2,rev)
image(1:ncol(r),1:nrow(r), as.matrix(t(r)), col=rgb(0:255,0,0,max=255),xlab="x",ylab="y",main="Red Channel")
image(1:ncol(g),1:nrow(g), as.matrix(t(g)), col=rgb(0,0:255,0,max=255),xlab="x",ylab="y",main="Green Channel")
image(1:ncol(b),1:nrow(b), as.matrix(t(b)), col=rgb(0,0,0:255,max=255),xlab="x",ylab="y",main="Blue Channel")
par(mfrow=c(1,1))
colmean_R <- colMeans(r)
colmean_G <- colMeans(g)
colmean_B <- colMeans(b)
plot(colmean_R, main="3 Color Dimensions", ylab="", type="l", col="red",ylim=c(0,1))
lines(colmean_G,col="green")
lines(colmean_B,col="blue")
legend("topleft", c("R","G","B"), fill=c("red","green","blue"))
r_top<-r[1:256,1:512]
r_bottom<-r[257:512,1:512]
r_diff<-abs(r_top-r_bottom)
diff_image_r<-as.raster(r_diff)
par(mfrow=c(2,1))
image(1:ncol(r),1:nrow(r), as.matrix(t(r)), col=rgb(0:255,0,0,max=255),xlab="x",ylab="y",main="Before Differentiation")
image(1:ncol(r_diff),1:nrow(r_diff), as.matrix(t(r_diff)), col=rgb(0:255,0,0,max=255),xlab="x",ylab="y",main="After Differentiation")
g_top<-g[1:256,1:512]
g_bottom<-g[257:512,1:512]
g_diff<-abs(g_top-g_bottom)
diff_image_g<-as.raster(g_diff)
par(mfrow=c(2,1))
image(1:ncol(g),1:nrow(g), as.matrix(t(g)), col=rgb(0,0:255,0,max=255),xlab="x",ylab="y",main="Before Differentiation")
image(1:ncol(g_diff),1:nrow(g_diff), as.matrix(t(g_diff)), col=rgb(0,0:255,0,max=255),xlab="x",ylab="y",main="After Differentiation")
b_top<-b[1:256,1:512]
b_bottom<-b[257:512,1:512]
b_diff<-abs(b_top-b_bottom)
diff_image_b<-as.raster(b_diff)
par(mfrow=c(2,1))
image(1:ncol(b),1:nrow(b), as.matrix(t(b)), col=rgb(0,0,0:255,max=255),xlab="x",ylab="y",main="Before Differentiation")
image(1:ncol(b_diff),1:nrow(b_diff), as.matrix(t(b_diff)), col=rgb(0,0,0:255,max=255),xlab="x",ylab="y",main="After Differentiation")
library(EBImage)
par(mfrow=c(2,2))
size <- 5
mfiltered_surface_5 <- medianFilter(surface,size = size)
last_5 <- as.raster(mfiltered_surface_5)
size <- 11
mfiltered_surface_11 <- medianFilter(surface,size = size)
last_11 <- as.raster(mfiltered_surface_11)
size <- 31
mfiltered_surface_31 <- medianFilter(surface,size = size)
last_31 <- as.raster(mfiltered_surface_31)
plot(surface_ras)
text(x=270,y=28,labels=expression(bold("Original Image")))
plot(last_5)
text(x=270,y=28,labels=expression(bold("5x5 Filtered")))
plot(last_11)
text(x=270,y=28,labels=expression(bold("11x11 Filtered")))
plot(last_31)
text(x=270,y=28,labels=expression(bold("31x31 Filtered")))
library(extraDistr)
library(jpeg)
library(car)
grey<- readJPEG("grey.jpg")
str(grey)
grey_image<-grey[,,1]
par(mfrow=c(1,1))
hist(grey_image,main="Frequency of Pixel Values",xlab="Pixel Values",ylab="Frequency")
getmode <- function(v) {
uniqv <- unique(v)
uniqv[which.max(tabulate(match(v, uniqv)))]
}
a <- min(grey_image)
b <- max(grey_image)
c <- getmode(grey_image)
m <- mean(grey_image)
s <- sd(grey_image)
par(mfrow=c(1,1))
hist(rnorm(length(grey_image),m,s), main = "Original vs Normal vs Triangular",xlab = "Pixel Value",col = rgb(0,0,0.9,0.5),xlim=c(0,1))
hist(rtriang(length(grey_image),a,b,c),col=rgb(0.9,0,0,0.5),add=TRUE )
hist(grey_image,add=TRUE,col=rgb(0,0.9,0,0.5))
legend("topleft", c("Triangular","Original","Normal"), fill=c(rgb(0.9,0,0,0.5),rgb(0,0.9,0,0.5),rgb(0,0,0.9,0.5)))
set.seed(366)
x <- sample(grey_image,5000)
set.seed(366)
ks.test(x,rtriang(length(x),min(x),max(x),getmode(x)))
min <- min(grey_image)
max <- max(grey_image)
mode <- getmode(grey_image)
cat("max:",max," ")
cat("min:",min," ")
cat("mode:",mode)
alpha <- 0.001
lower <- qtriang(alpha,a,b,c)#qnorm(alpha,m,s)
upper <- qtriang(1-alpha,a,b,c)#qnorm(1-alpha,m,s)
grey_image_out <- grey_image
number_out <- as.integer(0)
for(i in 1:nrow(grey_image)){
for(j in 1:ncol(grey_image)){
if(grey_image[i,j] > upper | grey_image[i,j] < lower){
grey_image_out[i,j] <- 0
number_out <- number_out +1
}
}
}
number_out
par(mfrow=c(1,2))
plot(as.raster(grey_image),main= "Original")
text(x=230,y=600,labels=expression(bold("Original")))
plot(as.raster(grey_image_out),main="Without Outliers")
text(x=230,y=600,labels=expression(bold("Without Outliers")))
alpha <- 0.001
grey_image_out_patch <- grey_image
number_out_patch <- as.integer(0)
lower_patch <- matrix(0,10,10)
upper_patch <- matrix(0,10,10)
for(i in 1:10){
for(j in 1:10){
min_p<- min(grey_image[((i-1)*51+1):(i*51),((j-1)*51+1):(j*51)])
max_p<- max(grey_image[((i-1)*51+1):(i*51),((j-1)*51+1):(j*51)])
mode_p<- getmode(grey_image[((i-1)*51+1):(i*51),((j-1)*51+1):(j*51)])
lower_patch[i,j] <- qtriang(alpha,a=min_p,b=max_p,c=mode_p)
upper_patch[i,j] <- qtriang(1-alpha,a=min_p,b=max_p,c=mode_p)
}
}
for(i in 1:509){
for(j in 1:509){
upper_p <- upper_patch[floor(i/51)+1,floor(j/51)+1]
lower_p <- lower_patch[floor(i/51)+1,floor(j/51)+1]
if(grey_image[i,j] > upper_p | grey_image[i,j] < lower_p){
grey_image_out_patch[i,j] <- 0
number_out_patch <- number_out_patch +1
}
}
}
number_out_patch
par(mfrow=c(1,2))
plot(as.raster(grey_image),main= "Original")
text(x=230,y=600,labels=expression(bold("Original")))
plot(as.raster(grey_image_out_patch),main="Without Outliers")
text(x=230,y=600,labels=expression(bold("Without Outliers")))